﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YunCMS.BLL;
using YunCMS.Model;
using YunCMS.Common;


namespace YunCMS.Web.Admin.User
{
    public partial class UserList : System.Web.UI.Page
    {
        private YCUserBLL _bll = null;

        protected YCUserBLL BLL
        {
            get
            {
                if (_bll == null)
                {
                    _bll = new YCUserBLL();
                }
                return _bll;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string action = Request.QueryString["action"];
                int id;               

                if (!string.IsNullOrEmpty(action))
                {
                    string _id = Request.QueryString["id"];

                    if (!int.TryParse(_id, out id))
                        id = 0;
                    if (id != 0)
                        HandleAction(action, id);
                }
                BindControls(keywordsTB.Text.Trim());
            }
        }

        private void BindControls(string account)
        {
            IList<YCUser> list = new List<YCUser>();
            list = BLL.GetList(account);
            DataListRP.DataSource = list;
            DataListRP.DataBind();
        }

        private void HandleAction(string action, int id)
        {
            if (action == "delete")
            {
                BLL.Remove(id);
            }
            else if (action == "active")
            {
                string qval = Request.QueryString["val"];
                if (!string.IsNullOrEmpty(qval))
                {
                    bool val = Convert.ToBoolean(qval);
                    BLL.UpdateDeactive(id, !val);
                }
            }
        }

        private IList<int> AllSelectedItems()
        {
            List<int> ids = new List<int>() { };
            for (int i = 0; i < DataListRP.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)DataListRP.Items[i].FindControl("idCHB");
                if (cb.Checked)
                {
                    int id = Convert.ToInt32(((Label)DataListRP.Items[i].FindControl("idLabel")).Text);
                    ids.Add(id);
                }
            }
            return ids;
        }

        protected void searchBT_Click(object sender, EventArgs e)
        {
            BindControls(keywordsTB.Text.Trim());
        }

        protected void deleteAllBT_Click(object sender, EventArgs e)
        {
            IList<int> ids = AllSelectedItems();
            foreach (int id in ids)
            {
                BLL.Remove(id);
            }
            BindControls(keywordsTB.Text.Trim());
        }

        protected void activeAllBT_Click(object sender, EventArgs e)
        {
            IList<int> ids = AllSelectedItems();
            foreach (int id in ids)
            {
                BLL.UpdateDeactive(id, true);
            }
            BindControls(keywordsTB.Text.Trim());
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

        }
    }
}